home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / ViePratique / ArchiFacile / ArchiFacileSetup.exe / {app} / nw.pak / Unnamed File 001162.txt < prev    next >
Text File  |  2014-10-14  |  6KB  |  179 lines

  1. // Copyright (c) 2012 Intel Corp
  2. // Copyright (c) 2012 The Chromium Authors
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. //  in the Software without restriction, including without limitation the rights
  7. //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
  8. // pies of the Software, and to permit persons to whom the Software is furnished
  9. //  to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in al
  12. // l copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
  15. // PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
  16. // S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  17. //  OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
  18. // ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. //  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20.  
  21. var v8_util = process.binding('v8_util');
  22.  
  23. function MenuItem(option) {
  24.   if (typeof option != 'object')
  25.     throw new String('Invalid option.');
  26.  
  27.   if (!option.hasOwnProperty('type'))
  28.     option.type = 'normal';
  29.  
  30.   if (option.type != 'normal' &&
  31.       option.type != 'checkbox' &&
  32.       option.type != 'separator')
  33.     throw new String('Invalid MenuItem type: ' + option.type);
  34.  
  35.   if (option.type == 'normal' || option.type == 'checkbox') {
  36.     if (option.type == 'checkbox')
  37.       option.checked = Boolean(option.checked);
  38.  
  39.     if (!option.hasOwnProperty('label'))
  40.       throw new String('A normal MenuItem must have a label');
  41.     else
  42.       option.label = String(option.label);
  43.  
  44.     if (option.hasOwnProperty('icon')) {
  45.       option.shadowIcon = String(option.icon);
  46.       option.icon = nw.getAbsolutePath(option.icon);
  47.     }
  48.  
  49.     if (option.hasOwnProperty('tooltip'))
  50.       option.tooltip = String(option.tooltip);
  51.  
  52.     if (option.hasOwnProperty('enabled'))
  53.       option.enabled = Boolean(option.enabled);
  54.  
  55.     if (option.hasOwnProperty('submenu')) {
  56.       if (v8_util.getConstructorName(option.submenu) != 'Menu')
  57.         throw new String("'submenu' must be a valid Menu");
  58.  
  59.       // Transfer only object id
  60.       v8_util.setHiddenValue(this, 'submenu', option.submenu);
  61.       option.submenu = option.submenu.id;
  62.     }
  63.  
  64.     if (option.hasOwnProperty('click')) {
  65.       if (typeof option.click != 'function')
  66.         throw new String("'click' must be a valid Function");
  67.       else
  68.         this.click = option.click;
  69.     }
  70.   } else if (option.type == 'separator') {
  71.     option = {
  72.       type: 'separator'
  73.     };
  74.   }
  75.  
  76.   v8_util.setHiddenValue(this, 'option', option);
  77.   nw.allocateObject(this, option);
  78.  
  79.   // All properties must be set after initialization.
  80.   if (!option.hasOwnProperty('icon'))
  81.     option.shadowIcon = '';
  82.   if (!option.hasOwnProperty('tooltip'))
  83.     option.tooltip = '';
  84.   if (!option.hasOwnProperty('enabled'))
  85.     option.enabled = true;
  86.   if (!option.hasOwnProperty('key'))
  87.     option.key = "";
  88.   if (!option.hasOwnProperty('modifiers'))
  89.     option.modifiers = "";
  90. }
  91. require('util').inherits(MenuItem, exports.Base);
  92.  
  93. MenuItem.prototype.__defineGetter__('type', function() {
  94.   return this.handleGetter('type');
  95. });
  96.  
  97. MenuItem.prototype.__defineSetter__('type', function() {
  98.   throw new String("'type' is immutable at runtime");
  99. });
  100.  
  101. MenuItem.prototype.__defineGetter__('label', function() {
  102.   return this.handleGetter('label');
  103. });
  104.  
  105. MenuItem.prototype.__defineSetter__('label', function(val) {
  106.   this.handleSetter('label', 'SetLabel', String, val);
  107. });
  108.  
  109. MenuItem.prototype.__defineGetter__('icon', function() {
  110.   return this.handleGetter('shadowIcon');
  111. });
  112.  
  113. MenuItem.prototype.__defineSetter__('icon', function(val) {
  114.   v8_util.getHiddenValue(this, 'option').shadowIcon = String(val);
  115.   var real_path = val == '' ? '' : nw.getAbsolutePath(val);
  116.   this.handleSetter('icon', 'SetIcon', String, real_path);
  117. });
  118.  
  119. MenuItem.prototype.__defineGetter__('tooltip', function() {
  120.   return this.handleGetter('tooltip');
  121. });
  122.  
  123. MenuItem.prototype.__defineSetter__('tooltip', function(val) {
  124.   this.handleSetter('tooltip', 'SetTooltip', String, val);
  125. });
  126.  
  127. MenuItem.prototype.__defineGetter__('checked', function() {
  128.   if (this.type != 'checkbox')
  129.     return undefined;
  130.  
  131.   return this.handleGetter('checked');
  132. });
  133.  
  134. MenuItem.prototype.__defineSetter__('checked', function(val) {
  135.   if (this.type != 'checkbox')
  136.     throw new String("'checked' property is only available for checkbox");
  137.  
  138.   this.handleSetter('checked', 'SetChecked', Boolean, val);
  139. });
  140.  
  141. MenuItem.prototype.__defineGetter__('enabled', function() {
  142.   return this.handleGetter('enabled');
  143. });
  144.  
  145. MenuItem.prototype.__defineSetter__('enabled', function(val) {
  146.   this.handleSetter('enabled', 'SetEnabled', Boolean, val);
  147. });
  148.  
  149. MenuItem.prototype.__defineGetter__('submenu', function() {
  150.   return v8_util.getHiddenValue(this, 'submenu');
  151. });
  152.  
  153. MenuItem.prototype.__defineSetter__('submenu', function(val) {
  154.   if (v8_util.getConstructorName(val) != 'Menu')
  155.     throw new String("'submenu' property requries a valid Menu");
  156.  
  157.   v8_util.setHiddenValue(this, 'submenu', val);
  158.   nw.callObjectMethod(this, 'SetSubmenu', [ val.id ]);
  159. });
  160.  
  161. MenuItem.prototype.handleEvent = function(ev) {
  162.   if (ev == 'click') {
  163.     // Automatically flag the 'checked' property.
  164.     if (this.type == 'checkbox') {
  165.       var option = v8_util.getHiddenValue(this, 'option');
  166.       option.checked = !option.checked;
  167.     }
  168.  
  169.     // Emit click handler
  170.     if (typeof this.click == 'function')
  171.       this.click();
  172.   }
  173.  
  174.   // Emit generate event handler
  175.   exports.Base.prototype.handleEvent.apply(this, arguments);
  176. }
  177.  
  178. exports.MenuItem = MenuItem;
  179.